package com.saianfu.flowable

import com.saianfu.flowable.attendence.ApplyResult
import com.saianfu.flowable.attendence.ApplyType
import org.flowable.engine.*
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration
import org.junit.jupiter.api.Test

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach

internal class AttendanceFlowableExTest {
    lateinit var cfg: StandaloneProcessEngineConfiguration
    lateinit var processEngine: ProcessEngine
    lateinit var repositoryService: RepositoryService
    lateinit var runtimeService: RuntimeService
    lateinit var historyService: HistoryService
    lateinit var taskService: TaskService


    @BeforeEach
    fun setUp() {
        cfg = StandaloneProcessEngineConfiguration().also {
            it.jdbcPassword = "123456"
            it.jdbcUsername = "root"
            it.jdbcUrl = "jdbc:mysql://127.0.0.1:3306/flowable?" +
                    "useUnicode=true&characterEncoding=utf-8" +
                    "&useSSL=true" +
                    "&serverTimezone=UTC" +
                    "&nullCatalogMeansCurrent=true" +
                    "&allowMultiQueries=true"
            it.jdbcDriver = "com.mysql.cj.jdbc.Driver"
        }

        processEngine = cfg.buildProcessEngine()
        repositoryService = processEngine.repositoryService
        runtimeService = processEngine.runtimeService
        historyService = processEngine.historyService
        taskService = processEngine.taskService
//        val deployment = repositoryService.createDeployment()
//                .addClasspathResource("processes//Approve003.bpmn20.xml")
//                .deploy()
    }


    @Test
    fun createApply() {
        AttendanceFlowableEx.createApply(runtimeService, ApplyType.Leave,
                listOf(Pair("科室主任", "officeman"), Pair("部门经理", "deptman"),
                        Pair("分管领导", "breanchman"), Pair("副总经理", "viceman"), Pair("总经理", "generalman")
                )) {
            println(it)
        }
    }

    @Test
    fun listActiveApply() {
        AttendanceFlowableEx.listActiveApply(taskService, mapOf(), "officeman", "desc", 2, 3)
                .forEach {
                    println("listActiveApply:$it")
                }
    }

    @Test
    fun cancelApply() {
        AttendanceFlowableEx.cancelApply(runtimeService, "15001", "撤销流程")
    }

    @Test
    fun handleApply() {
        AttendanceFlowableEx.handleApply(taskService, "35001", "officeman33", ApplyResult.Rejected) { nodeName, rs, finished ->
            println("$nodeName,$rs,$finished")
        }
    }

    @Test
    fun resetAssign() {
        AttendanceFlowableEx.resetAssign(taskService, "35001", "officeman", "officeman33")
    }

    @Test
    fun listHistoryApply() {
        AttendanceFlowableEx.listHistoryApply(historyService, "officeman", 0, 30)
                .forEach {
                    println("listHistoryApply:$it")
                }
    }
}